package fifteenth.取得类的结构;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class 使用Modifier还原修饰符 {
    //自己敲的
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> c = Class.forName("fifteenth.取得类的结构.Person");
        Method[] m = c.getMethods();
        for (int i = 0; i < m.length; i++) {
            int modifiers = m[i].getModifiers();
            Class<?> returnType = m[i].getReturnType();
            Class<?>[] p = m[i].getParameterTypes();
            System.out.print(Modifier.toString(modifiers) + " ");
            System.out.print(returnType + " ");
            System.out.print(m[i].getName());
            System.out.print("(");
            for (int j = 0; j < p.length; j++) {
                System.out.print(p[j].getName() + " arg" + j);
                if (j < p.length - 1) {
                    System.out.print(",");
                }
            }
            Class<?>[] e = m[i].getExceptionTypes();
            if (e.length > 0) {
                System.out.print(") throws ");
            } else {
                System.out.print(")");
            }
            for (int j = 0; j < e.length; j++) {
                System.out.print(e[j].getName());
                if (j < e.length) {
                    System.out.print(",");
                }
            }
            System.out.println();
        }

    }
   /*
   跟着书敲的
   public static void main(String[] args) throws ClassNotFoundException {
        Class<?> c = null;
        c = Class.forName("fifteenth.取得类的结构.Person");
        // 获取全部方法
        Method[] m = c.getMethods();
        for (int i = 0; i < m.length; i++) {
            Class<?> r = m[i].getReturnType();// 获取返回类型
            Class<?>[] p = m[i].getParameterTypes();// 获取全部参数
            int modifier = m[i].getModifiers();// 获取修饰符
            Constructor<?>[] cons = c.getConstructors();// 获取构造方法
            System.out.print(Modifier.toString(modifier) + " ");
            System.out.print(m[i].getName());// 得到方法名称
            System.out.print("(");
            for (int j = 0; j < p.length; j++) {
                System.out.print(p[j].getName() + " " + "arg" + j);
                if (j < p.length - 1) {
                    System.out.print(",");
                }
            }
            Class<?>[] e = m[i].getExceptionTypes();//得到全部的异常抛出
            if(e.length>0){
                System.out.print(") throws " );
            }else{
                System.out.print(")");
            }
            for (int j = 0; j <e.length ; j++) {
                System.out.print(e[j].getName());
                if(j<e.length-1){
                    System.out.print(",");
                }
            }
            System.out.println();
        }
    }*/
}
/**
 * 程序不仅把Person类的方法输出，也把从Object类中继承来的方法同样进行输出
 *
 * 提示：
 * 开发工具是利用反射的原理
 * 在使用IDE进行程序开发的时候，都是带随笔提示功能，即使用一个“.",就可以找到一个类的全部方法，实际上这个功能是利用反射方式完成的
 */
/*
D:\WorkSpaces\Java\jdk-21\bin\java.exe "-javaagent:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=50392:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\WorkSpaces\JavaDevelopBase_start_329\out\production\JavaDevelopBase_start_329 fifteenth.取得类的结构.使用Modifier还原修饰符
public class java.lang.String sayHello(java.lang.String arg0,int arg1)
public int getAge()
public void setAge(int arg0)
public void sayChina()
public class java.lang.String getName()
public class java.lang.String toString()
public void setName(java.lang.String arg0)
public boolean equals(java.lang.Object arg0)
public native int hashCode()
public final native class java.lang.Class getClass()
public final native void notify()
public final native void notifyAll()
public final void wait(long arg0) throws java.lang.InterruptedException,
public final void wait(long arg0,int arg1) throws java.lang.InterruptedException,
public final void wait() throws java.lang.InterruptedException,

Process finished with exit code 0

 */